package com.mm.cloth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.mm.cloth.R;
import com.mm.cloth.model.Tool;

import java.util.List;

public class ToolAdapter extends RecyclerView.Adapter<ToolAdapter.ToolViewHolder> {

    private Context context;
    private List<Tool> toolList;
    private OnToolClickListener listener;

    public interface OnToolClickListener {
        void onToolClick(Tool tool);
    }

    public ToolAdapter(Context context, List<Tool> toolList) {
        this.context = context;
        this.toolList = toolList;
    }

    public void setOnToolClickListener(OnToolClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ToolViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_tool, parent, false);
        return new ToolViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ToolViewHolder holder, int position) {
        Tool tool = toolList.get(position);
        holder.tvToolName.setText(tool.getName());

        // 根据工具名称设置本地图标
        int iconResId = getToolIconResource(tool.getName());
        holder.ivTool.setImageResource(iconResId);

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onToolClick(tool);
            }
        });
    }

    private int getToolIconResource(String toolName) {
        switch (toolName) {
            case "抠图":
                return R.drawable.ic_matting;
            case "换脸":
                return R.drawable.ic_face_swap;
            case "换衣":
                return R.drawable.ic_clothes_swap;
            case "照片清晰":
                return R.drawable.ic_photo_enhance;
            case "AI绘画":
                return R.drawable.ic_ai_painting;
            case "老照片修复":
                return R.drawable.ic_photo_restore;
            case "证件照":
                return R.drawable.ic_id_photo;
            case "更多":
                return R.drawable.ic_more;
            default:
                return R.drawable.ic_tool_default;
        }
    }

    @Override
    public int getItemCount() {
        return toolList != null ? toolList.size() : 0;
    }

    public static class ToolViewHolder extends RecyclerView.ViewHolder {
        ImageView ivTool;
        TextView tvToolName;

        public ToolViewHolder(@NonNull View itemView) {
            super(itemView);
            ivTool = itemView.findViewById(R.id.ivTool);
            tvToolName = itemView.findViewById(R.id.tvToolName);
        }
    }
} 